"""
Case Type   : Operator
Case Name   : alter操作符的所有者为初始用户
Create At   : 2024.10.8
Owner       : wang-tianjing1
Description :
    1、设置参数support_extended_features为on
    2、创建新用户
    3、创建实现操作符的函数
    4、创建操作符
    5、修改为定义的操作符
    6、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
    6、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class OpreatorTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('dbuser')
        self.user_node = Node('dbuser')
        self.constant = Constant()

    def test_dolphin(self):
        text = '----step1:设置参数support_extended_features为on；expect：创建成功----'
        self.log.info(text)
        sql_cmd = f'alter system set support_extended_features to on;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('ALTER SYSTEM SET', sql_res, '执行失败' + text)
        self.log.info('-----重启数据库------')
        msg = self.sh_primary.restart_db_cluster()
        self.log.info(msg)
        status = self.sh_primary.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)

        text = '----step2:创建新用户；expect：创建成功----'
        self.log.info(text)
        sql_cmd = f'drop user if exists tom;'\
                  f'''create user tom password 'Test@123';'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE ROLE', sql_res, '执行失败' + text)

        text = '----step3:创建实现操作符的函数；expect：创建成功----'
        self.log.info(text)
        sql_cmd = f'drop function if exists test_procedure() cascade;'\
                  f'create function test_procedure(a int, b int) return bool as\n'\
                  f'begin\n'\
                  f'if a != b then return t;\n'\
                  f'else return f;\n'\
                  f'end if;\n'\
                  f'end;\n'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('DROP FUNCTION', sql_res, '执行失败' + text)
        self.assertIn('CREATE FUNCTION', sql_res, '执行失败' + text)

        text = '----step4:创建操作符;expect:成功----'
        self.log.info(text)
        sql_cmd1 = f'create operator #####('\
                   f'leftarg = int,'\
                   f'rightarg = int,'\
                   f'procedure = test_procedure);'\
                   f'restrict = eqsel,'\
                   f'join = eqjoinsel,'\
                   f'hashes, merges);'
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn("CREATE OPERATOR", sql_res1,'执行失败' + text)

        text = '----step5:修改为定义的操作符;expect:修改成功----'
        self.log.info(text)
        sql_cmd1 = f'alter operator ##### (int, int) owner to tom;'
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn("ALTER OPERATOR", sql_res1, '执行失败' + text)

    def tearDown(self):
        text = '--step6:清理环境;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop function test_procedure() cascade;"\
                  f'drop user tom;'
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn("DROP FUNCTION", sql_res, '执行失败' + text)
        self.assertIn("DROP ROLE", sql_res, '执行失败' + text)
        text = '----恢复参数----'
        self.log.info(text)
        sql_cmd = f'alter system set support_extended_features to off;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('ALTER SYSTEM SET', sql_res, '执行失败' + text)
        self.log.info('-----重启数据库------')
        msg = self.sh_primary.restart_db_cluster()
        self.log.info(msg)
        status = self.sh_primary.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
